package com.oracle.application.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.application.entity.PT;
import com.oracle.application.entity.PagePo;
import com.oracle.application.entity.Product;
import com.oracle.application.service.IPTService;
import com.oracle.application.service.IProductService;
import com.oracle.application.service.IProtypeService;
import com.oracle.application.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 图书表 前端控制器
 * </p>
 *
 * @author zhaoran
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService iProductService;
    @Autowired
    private IProtypeService iProtypeService;
    @Autowired
    private IPTService iptService;

    /**
     * 获取所有图书
     * @return
     */
    @RequestMapping("/getAllProduct")
    public R<PagePo> getAllProduct(@RequestParam(value = "psgeIndex",defaultValue = "1")Integer pageIndex,@RequestParam(value = "pageCount",defaultValue = "10")Integer pageCount){
        Page pages=new Page(pageIndex,pageCount);
        PagePo pagePo=new PagePo();

        Page page = iProductService.page(pages);
        pagePo.setData(page.getRecords());
        pagePo.setPageCount(pageCount);
        pagePo.setPageIndex(pageIndex);
        pagePo.setPageSize(Integer.parseInt(String.valueOf(page.getTotal())));
        return new R<PagePo>(200,"success",pagePo);
    }

    /**
     * 根据id获取单个图书
     * @param id
     * @return
     */
    @RequestMapping("/getProductById")
    public R<Product> getProductById(@RequestParam("id") Integer id ){
        System.out.println(id+"///////////////////");
        Product byId = iProductService.getById(id);
        return new R<Product>(200,"success",byId);
    }

    /**
     * 添加图书
     * @param product：图书
     * @param typeid：类型id
     * @return
     */
    @RequestMapping("/insetProduct")
    public R insetProduct(@RequestBody Product product, @RequestParam("typeid") Integer typeid){
        System.out.println(product.toString());
        iProductService.save(product);
        PT pt=new PT();
        pt.setPid(product.getId());
        pt.setTid(typeid);
        iptService.save(pt);
        return new R<Product>(200,"success",null);
    }

    /**
     * 删除图书
     * @param id：图书id
     * @return
     */
    @RequestMapping("/removeProduct")
    public R removeProduct(@RequestParam("id") Integer id){
        iProductService.removeById(id);
        PT pt=new PT();
        pt.setPid(id);
        QueryWrapper<PT> ptQueryWrapper=new QueryWrapper<>(pt);
        iptService.remove(ptQueryWrapper);
        return new R<Product>(200,"success",null);
    }

    /**
     * 修改图书
     * @param product：图书
     * @param typeid：类型id(无修改为0)
     * @return
     */
    @RequestMapping("/updateProduct")
    public R updateProduct(@RequestBody Product product,@RequestParam("typeid")  Integer typeid){
        if (typeid!=null&&typeid!=0){
            PT pt=new PT();
            pt.setPid(product.getId());
            QueryWrapper<PT> ptQueryWrapper=new QueryWrapper<>(pt);
            iptService.remove(ptQueryWrapper);
            PT pt2=new PT();
            pt2.setPid(product.getId());
            pt2.setTid(typeid);
            iptService.save(pt2);
        }
        iProductService.saveOrUpdate(product);
        return new R<Product>(200,"success",null);
    }

}
